﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CMN.Models;
using CMN.Models.Interfaces;
using CMN.Entity;
using System.Web.Routing;
using CMN.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using CMN.Controllers.Filters;
namespace CMN.Controllers
{
    [ExceptionFilter]
    [LoggerFilter]
    [UserAuthorize]
    public class Extend_CollectContentController : Controller
    {
        
        // 返回json字符串
        protected string returnJson = string.Empty;

        /// <summary>
        /// 转到View页面
        /// </summary>
        /// <returns></returns>
        public ViewResult Index()
        {
            return View("Index");//页面地址
        }

        /// <summary>
        /// 返回采集内容列表
        /// </summary>
        /// <returns>json</returns>
        public string List(string actionId)
        {
            //返回json数组
          
            int pageSize = (int)Utils.GetQueryAndFormString("limit", this, 4, 1, DataType.Int);
            int pageIndex = (int)Utils.GetQueryAndFormString("start", this, 9, 1, DataType.Int);
            string sort = (string)Utils.GetQueryAndFormString("sort", this, 100, 0, DataType.Str);
            QueryParam qp = new QueryParam();
            if (!string.IsNullOrEmpty(sort))
            {
                SortEntity sortEntity = null;
                List<SortEntity> sortList = new List<SortEntity>();
                sortList = (List<SortEntity>)JsonConvert.DeserializeObject(sort, typeof(List<SortEntity>));
                if (sortList.Count > 0)
                {
                    sortEntity = sortList[0];
                }

                qp.PageIndex = pageIndex / pageSize + 1;
                qp.PageSize = pageSize;
                if (sortEntity != null)
                    qp.Orderfld = string.Concat(sortEntity.Property, " ", sortEntity.Direction);
                qp.Where = "CollectSetId=" + actionId;
            }
            int recordNum = 0;
            IsoDateTimeConverter jsDataConvert= new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            jsDataConvert.DateTimeFormat = "yyyy-MM-dd";
            IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
            List<Extend_CollectContentEntity> extend_collectcontentList = Extend_CollectContentSv.List(qp, out recordNum);
            returnJson = string.Concat("{totalPorperty:", recordNum, ",root:", JsonConvert.SerializeObject(extend_collectcontentList, Formatting.Indented, jsDataConvert), "}");
            return returnJson;
 
        }

        /// <summary>
        /// 查看采集内容 
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
        public string Look(string actionId)
        {
           IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
           int intId = Utils.StringToInt(actionId, 0);
            Extend_CollectContentEntity extend_collectcontent = Extend_CollectContentSv.Look(intId);
            if (extend_collectcontent != null)
            {
                
                IsoDateTimeConverter jsDataConvert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
                jsDataConvert.DateTimeFormat = "yyyy-MM-dd hh:mm:ss";
                returnJson = string.Concat("{success:true,data:", JsonConvert.SerializeObject(extend_collectcontent, Formatting.Indented, jsDataConvert), "}");
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 更新采集内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
         [ValidateInput(false)]
        public string Update(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Extend_CollectContentEntity extend_collectcontent = new Extend_CollectContentEntity();
                extend_collectcontent.DataTable_Action = DataTableAction.Update;
                extend_collectcontent.Id = intId;
                extend_collectcontent.ChannelId = (Int32)Utils.GetQueryAndFormString("ChannelId", this, 10,  1 , DataType.Int);
                extend_collectcontent.CollectSetId = (Int32)Utils.GetQueryAndFormString("CollectSetId", this, 10,  1 , DataType.Int);
                extend_collectcontent.Title = (String)Utils.GetQueryAndFormString("Title", this, 150,  1 , DataType.Str);
                extend_collectcontent.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 200,  0, DataType.Str);
                extend_collectcontent.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23,  1 , DataType.Dat);
                extend_collectcontent.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 360,  0, DataType.Str);
                extend_collectcontent.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20,  0 , DataType.Str);
                extend_collectcontent.Author = (String)Utils.GetQueryAndFormString("Author", this, 30,  0, DataType.Str);
                extend_collectcontent.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10,  1 , DataType.Int);
                extend_collectcontent.IsPass = (bool)Utils.GetQueryAndFormString("IsPass", this, 6,  1 , DataType.Bool);
                extend_collectcontent.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  1 , DataType.Int);
                extend_collectcontent.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 330,  0, DataType.Str);
                extend_collectcontent.Img = (String)Utils.GetQueryAndFormString("Img", this, 300, 0, DataType.Str);
                extend_collectcontent.TextContent = (String)Utils.GetQueryAndFormString("TextContent", this, 2147483647,  1 , DataType.Str);
                extend_collectcontent.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 6, 1, DataType.Bool);
                extend_collectcontent.Remark = (String)Utils.GetQueryAndFormString("Remark", this, 2147483647, 0, DataType.Str);
                extend_collectcontent.Size = (String)Utils.GetQueryAndFormString("Size", this, 30, 0, DataType.Str);
                IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
                if (Extend_CollectContentSv.Update(extend_collectcontent) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
           
            return returnJson;
        }

        /// <summary>
        /// 新增采集内容
        /// </summary>
        /// <returns>json</returns>
         [ValidateInput(false)]
        public string Insert()
        {
            Extend_CollectContentEntity extend_collectcontent = new Extend_CollectContentEntity();
            extend_collectcontent.DataTable_Action = DataTableAction.Insert;
            extend_collectcontent.ChannelId = (Int32)Utils.GetQueryAndFormString("ChannelId", this, 10,  1 , DataType.Int);
            extend_collectcontent.CollectSetId = (Int32)Utils.GetQueryAndFormString("CollectSetId", this, 10,  1 , DataType.Int);
            extend_collectcontent.Title = (String)Utils.GetQueryAndFormString("Title", this, 150,  1 , DataType.Str);
            extend_collectcontent.Tags = (String)Utils.GetQueryAndFormString("Tags", this, 200,  0, DataType.Str);
            extend_collectcontent.AddDate = (DateTime)Utils.GetQueryAndFormString("AddDate", this, 23,  1 , DataType.Dat);
            extend_collectcontent.Summary = (String)Utils.GetQueryAndFormString("Summary", this, 360,  0, DataType.Str);
            extend_collectcontent.Editor = (String)Utils.GetQueryAndFormString("Editor", this, 20,  0 , DataType.Str);
            extend_collectcontent.Author = (String)Utils.GetQueryAndFormString("Author", this, 30,  0, DataType.Str);
            extend_collectcontent.ViewNum = (Int32)Utils.GetQueryAndFormString("ViewNum", this, 10,  1 , DataType.Int);
            extend_collectcontent.IsPass = (bool)Utils.GetQueryAndFormString("IsPass", this, 1,  1 , DataType.Byte);
            extend_collectcontent.OrderNum = (Int32)Utils.GetQueryAndFormString("OrderNum", this, 10,  1 , DataType.Int);
            extend_collectcontent.SourceFrom = (String)Utils.GetQueryAndFormString("SourceFrom", this, 330,  0, DataType.Str);
            extend_collectcontent.TextContent = (String)Utils.GetQueryAndFormString("TextContent", this, 2147483647,  1 , DataType.Str);
            extend_collectcontent.Enabled = (bool)Utils.GetQueryAndFormString("Enabled", this, 1,  1 , DataType.Byte);
            extend_collectcontent.Remark = (String)Utils.GetQueryAndFormString("Remark", this, 2147483647, 0, DataType.Str);
            extend_collectcontent.Img = (String)Utils.GetQueryAndFormString("Img", this, 300, 0, DataType.Str);
            extend_collectcontent.Size = (String)Utils.GetQueryAndFormString("Size", this, 30, 0, DataType.Str);
            IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
           if (Extend_CollectContentSv.Insert(extend_collectcontent) == "")
            {
                returnJson = "{success:'true'}";
            }
            else
                returnJson = "{success:'false'}";
            return returnJson;
        }

        /// <summary>
        /// 删除采集内容
        /// </summary>
        /// <param name="id"></param>
        /// <returns>json</returns>
         public string Delete(string actionId)
        {
            int intId = Utils.StringToInt(actionId, 0);
            if (intId != 0)
            {
                Extend_CollectContentEntity extend_collectcontent = new Extend_CollectContentEntity();
                extend_collectcontent.Id = intId;
                IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
                if (Extend_CollectContentSv.Delete(extend_collectcontent) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";
            }
            else
            {
                returnJson = "{success:'false'}";
            }
            return returnJson;
        }

        /// <summary>
        /// 批量删除采集内容
        /// </summary>
        /// <param name="idList">id列表 234_4_5</param>
        /// <returns>json</returns>
         public string DeleteList(string actionId)
        {
            Common.RegExpression.NumAndBarRegExpression regex = new CMN.Common.RegExpression.NumAndBarRegExpression();
            if (regex.IsMatch(actionId))
            {

                IDataService<Extend_CollectContentEntity> Extend_CollectContentSv = ServiceBuilder.BuildExtend_CollectContentService();
                if (Extend_CollectContentSv.DeleteList(actionId) == "")
                {
                    returnJson = "{success:'true'}";
                }
                else
                    returnJson = "{success:'false'}";

            }
            else
            {
                returnJson = "{success:'false',message:'参数类型不正确'}";
            }
            return returnJson;
        }

    }
}
